<?php
class base extends Controller{
	public $layout = "site/layout.php";

	public function init(){
		parent::init();

		$this->static = '/static/admin';
		$this->version = time();

		$this->admin_id = $_SESSION['site_admin']['id'];
		$this->admin_mnm = get_cookie('site_admin_mnm');


		$this->module = arg('m');
		$this->controller = arg('c');
		$this->action = arg('a');
		$this->routeUrl = $this->module.'/'.$this->controller;
		
		$this->fmdo = arg('do');
		$this->tab = arg('tab') ? arg('tab') : 0 ;
		$this->doids = arg('doids');
		$this->keyword = arg('keyword');
		$this->sort = arg('sort');
		$this->list_order = arg('list_order');
		$this->time_s = arg('time_s');
		$this->time_e = arg('time_e');
		$this->shifu_id = arg('shifu_id');

		$this->statusRadio = array('1'=>'显示','0'=>'隐藏');
		$this->sexRadio = array('1'=>'男','0'=>'女');
		$this->yesRadio = array('0'=>'关闭','1'=>'开启');

		$this->moshiRadio = array('1'=>'自由接单','2'=>'自动派单','3'=>'循环派单');
		$this->userLevels = array('1'=>'报修人','2'=>'报修组长','3'=>'师傅','4'=>'师傅组长');
		$this->orderStatus = array('1'=>'新订单','2'=>'已派单','3'=>'已接单','4'=>'维修中','5'=>'已完成','6'=>'已评价','7'=>'已结算','99'=>'已取消');

		
		if($this->admin_id){
			$this->admin = D('site_admin')->findById($this->admin_id);
			$this->site = D('site')->findById($this->admin['site_id']);
			$this->site_setting = unserialize($this->site['setting']);

			$this->cond_base = " and site_id={$this->site['id']}";
			$this->cond_city = " and (city_id IN ({$this->site['city_ids']}))";
			$this->cond_area = " and (city_id IN ({$this->site['city_ids']}) or area_id IN ({$this->site['city_ids']}))";
		}
		$this->setting = D('sys_setting')->get_setting();

		$this->checkLogin();
		$this->get_menus();
	}

	public function checkLogin(){
		if($this->routeUrl != "{$this->module}/login"){
			 if(!$this->admin_id){
				 $this->redirect("/login/index");
			 }
		 }
	 }
	
	 public function get_menus(){
		if(!$this->admin) return;
		$type = 2;
		if($this->admin['is_admin']){
			$cond = "type={$type} and status=1";
			$menus = D('sys_access')->getList($cond);
		}else{
			$role = D('sys_role')->info($this->admin['role_id']);
			$cond = "type={$type} and status=1 and id IN ({$role['access_ids']})";
			$menus = D('sys_access')->getList($cond);
		}
		$this->routeUri = $this->controller . '/' . $this->action;
		// dump($menus);exit;

		$action_arr = ['index','add','edit','auth','pwd'];
		if(in_array($this->action,$action_arr)){
			$this->routeUri = $this->controller . '/index';
		}

		$this->group = D('sys_access')->get_groupId($type, $this->routeUri);
		$this->menus = D('sys_access')->get_menuList($menus,$this->group,$this->routeUri);
		// dump($this->menus);exit;
	}
 

    public function redirect($url){
		$arr = explode('/',$url);
    	if(count($arr) == 3){
			$url = "/{$this->module}{$url}";
		}
		header("Location:" . $url);
		exit;
	}

	protected function renderJson($code = 1, $msg = '', $url = '', $data = [])
    {
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode(compact('code', 'msg', 'url', 'data')));
        // return compact('code', 'msg', 'url', 'data');
    }

    protected function renderSuccess($msg = 'success', $url = '', $data = [])
    {
        return $this->renderJson(1, $msg, $url, $data);
    }

    protected function renderError($msg = 'error', $url = '', $data = [])
    {
        return $this->renderJson(0, $msg, $url, $data);
    }

    protected function postData($key)
    {
        return arg($key);
    }

    protected function isAjax(){
	  if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
	    return true;
	  }else{
	    return false;
	  }
	}

	//cookie
    // public function set_cookie($name,$value) {
    //     $cookie=PHPCookie::getInstance();
    //     return $cookie->set($name,$value);
    // }
    // public function get_cookie($name) {
    //     $cookie=PHPCookie::getInstance();
    //     return $cookie->get($name);
    // }
    // public function del_cookie($name) {
    //     $cookie=PHPCookie::getInstance();
    //     return $cookie->delete($name);
    // }

	
} 